package ynu.edu.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.edu.elmspringboot.po.Food;
import ynu.edu.elmspringboot.service.FoodService;
import ynu.edu.elmspringboot.utils.PageResult;

import java.util.List;

@RestController
@RequestMapping("/FoodController")
@Api(tags = "菜品管理")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @ApiOperation("根据商家id查询菜品信息")
    @PostMapping("/listFoodByBusinessId")
    public List<Food> listFoodByBusinessId(Food food) {
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", food.getBusinessId())
                .orderByAsc("food_id");
        return foodService.list(queryWrapper);
    }

    @ApiOperation("根据菜品id查询菜品信息")
    @PostMapping("/getFoodById")
    public Food getFoodById(Integer foodId) {
        return foodService.getById(foodId);
    }

    @ApiOperation("分页查询菜品列表")
    @GetMapping("/getFoodPage")
    public PageResult<Food> getFoodPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("菜品名称") @RequestParam(required = false) String foodName,
            @ApiParam("商家ID") @RequestParam(required = false) Integer businessId) {
        
        Page<Food> page = new Page<>(current, size);
        IPage<Food> result = foodService.getFoodPage(page, foodName, businessId);
        return PageResult.fromIPage(result);
    }

    @ApiOperation("根据商家ID分页查询菜品")
    @GetMapping("/getFoodPageByBusiness")
    public PageResult<Food> getFoodPageByBusiness(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("商家ID") @RequestParam Integer businessId) {
        
        Page<Food> page = new Page<>(current, size);
        IPage<Food> result = foodService.getFoodPageByBusiness(page, businessId);
        return PageResult.fromIPage(result);
    }

    @ApiOperation("根据价格范围分页查询菜品")
    @GetMapping("/getFoodPageByPriceRange")
    public PageResult<Food> getFoodPageByPriceRange(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("最低价格") @RequestParam(required = false) Double minPrice,
            @ApiParam("最高价格") @RequestParam(required = false) Double maxPrice) {
        
        Page<Food> page = new Page<>(current, size);
        IPage<Food> result = foodService.getFoodPageByPriceRange(page, minPrice, maxPrice);
        return PageResult.fromIPage(result);
    }
}